package com.lcn.controller;

import com.lcn.base.BaseInfoProperties;
import com.lcn.grace.result.GraceJSONResult;
import com.lcn.grace.result.ResponseStatusEnum;
import com.lcn.pojo.netty.NettyServerNode;
import com.lcn.service.ChatMessageService;
import com.lcn.utils.JsonUtils;
import com.lcn.utils.PagedGridResult;
import jakarta.annotation.Resource;
import org.apache.curator.framework.CuratorFramework;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/chat")
public class ChatController extends BaseInfoProperties {

    @Resource
    private ChatMessageService chatMessageService;

    @Resource(name = "curatorClient")
    private CuratorFramework zkClient;

    /**
     * 查询未读消息
     *
     * @param myId
     * @return
     */
    @PostMapping("/getMyUnReadCounts")
    public GraceJSONResult getMyUnReadCounts(String myId) {
        Map map = redis.hgetall(CHAT_MSG_LIST + ":" + myId);
        return GraceJSONResult.ok(map);
    }

    /**
     * 清除未读消息
     *
     * @param myId
     * @param oppositeId
     * @return
     */
    @PostMapping("/clearMyUnReadCounts")
    public GraceJSONResult clearMyUnReadCounts(String myId, String oppositeId) {
        redis.setHashValue(CHAT_MSG_LIST + ":" + myId, oppositeId, "0");
        return GraceJSONResult.ok();
    }

    /**
     * 分页查询历史聊天记录
     *
     * @param senderId
     * @param receiverId
     * @param page
     * @param pageSize
     * @return
     */
    @PostMapping("/list/{senderId}/{receiverId}")
    public GraceJSONResult list(@PathVariable("senderId") String senderId,
                                @PathVariable("receiverId") String receiverId,
                                @RequestParam(defaultValue = "1") Integer page,
                                @RequestParam(defaultValue = "20") Integer pageSize) {
        PagedGridResult pagedGridResult = chatMessageService.list(senderId, receiverId, page, pageSize);
        return GraceJSONResult.ok(pagedGridResult);
    }

    /**
     * 获取使用人数最少的netty服务器
     *
     * @return
     */
    @PostMapping("/getNettyOnlineInfo")
    public GraceJSONResult getNettyOnlineInfo() throws Exception {
        String path = "/server-list";
        // 获取所有的netty服务器节点
        List<String> nodeStrList = zkClient.getChildren().forPath(path);
        if (nodeStrList == null || nodeStrList.size() == 0) {
            return GraceJSONResult.errorCustom(ResponseStatusEnum.SERVER_NULL);
        }
        List<NettyServerNode> nodeList = new ArrayList<>();
        // 转化为对象
        for (String node : nodeStrList) {
            String json = new String(zkClient.getData().forPath(path + "/" + node));
            NettyServerNode nettyServerNode = JsonUtils.jsonToPojo(json, NettyServerNode.class);
            nodeList.add(nettyServerNode);
        }
        // 最小人数节点
        NettyServerNode minNode = nodeList.stream()
                .min(Comparator.comparing(NettyServerNode::getOnlineCount))
                .get();

        return GraceJSONResult.ok(minNode);
    }

}
